package TestDemo;

import javaBean.Emp;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import tool.ThingManage;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
@SuppressWarnings(value ="all")
public class Test1 {
    public static void main(String[] args) throws IOException {


    }

    /**
     *查询所以数据
     * @throws IOException
     */

    @Test
    public void findDate() throws IOException {
        ThingManage.journal();
        SqlSession sqlSession = ThingManage.startThing();
        List<Object> objects = sqlSession.selectList("EmpMapper.findAllData");
        for (Object object : objects) {
            System.out.println(object);
        }
    }

    /**
     * 插入数据
     * @throws IOException
     */
   public void insertDate() throws IOException {
       ThingManage.journal();
       SqlSession sqlSession = ThingManage.startThing();
       int objects = sqlSession.update("EmpMapper.insertData");
       //提交事务
       sqlSession.commit();
    System.out.println(objects);
}

@Test
    public void update() throws IOException {
    ThingManage.journal();
    SqlSession sqlSession = ThingManage.startThing();
    int update = sqlSession.update("EmpMapper.update");
    //提交事务
        sqlSession.commit();
        System.out.println(update);
    }

    /**
     * 插入一条数据
     *
     * @throws IOException
     */
    @Test
    public void insertOneData() throws IOException {
        ThingManage.journal();
        SqlSession sqlSession = ThingManage.startThing();
        HashMap objectObjectHashMap = new HashMap();
        objectObjectHashMap.put("stu_name", "王五");
        objectObjectHashMap.put("stu_age", 52);
        objectObjectHashMap.put("stu_phone", "1864513");
        objectObjectHashMap.put("stu_exc", "女");
        int update = sqlSession.update("EmpMapper.insertdata1", objectObjectHashMap);
        sqlSession.commit();
        System.out.println("update = " + update);
    }
    /**
     * 更新一条数据
     *
     * @throws IOException
     */
    @Test
    public void updateOneData() throws IOException {
        ThingManage.journal();
        SqlSession sqlSession = ThingManage.startThing();
        Emp emp = new Emp();
        emp.setStu_name("李四");
        emp.setStu_age(32);
        emp.setStu_phone("156414");
        emp.setStu_exc("男");
        int update = sqlSession.update("EmpMapper.update1", emp);
        sqlSession.commit();
        System.out.println("update = " + update);
    }
    /**
     * 删除一条数据
     *
     * @throws IOException
     */
    @Test
    public void delOneData() throws IOException {
        ThingManage.journal();
        SqlSession sqlSession = ThingManage.startThing();
        int delete = sqlSession.delete("EmpMapper.delete2", 7);
        sqlSession.commit();
        System.out.println("delete = " + delete);
    }
    @Test
    public void testFindData() throws IOException {
        ThingManage.journal();
        SqlSession sqlSession = ThingManage.startThing();
        HashMap hashMap = new HashMap();
        hashMap.put("cols", "stu_name,stu_age");
        List<Object> objects = sqlSession.selectList("EmpMapper.findData3", hashMap);
        for (Object obj : objects) {
            System.out.println(obj);
        }
    }


    /**
     * 1、模糊查询
     */
    @Test
    public void findByName() throws IOException {
        ThingManage.journal();
        SqlSession sqlSession = ThingManage.startThing();
        HashMap hashMap = new HashMap();
        hashMap.put("stu_name","%三%");
        List<Object> objects = sqlSession.selectList("EmpMapper.findName", hashMap);
        for (Object obj : objects) {
            System.out.println(obj);
        }
    }
    /**
     * 2、找出某个区间的所有记录
     */
    @Test
    public void findSalaryTest() throws IOException {
        ThingManage.journal();
        SqlSession sqlSession = ThingManage.startThing();
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("minSal",6);
        stringObjectHashMap.put("maxSal",40);
        List<Object> objects = sqlSession.selectList("EmpMapper.findSalary", stringObjectHashMap);
        for (Object obj : objects) {
            System.out.println(obj);
        }
    }
    /**
     * 3、同时删除多条数据记录
     */
    @Test
    public void deleteByIdsTest() throws IOException {ThingManage.journal();
        SqlSession sqlSession = ThingManage.startThing();
        Integer [] ids={8,19,21};
        int delete = sqlSession.delete("EmpMapper.deleteByIds", ids);
        sqlSession.commit();
        System.out.println("delete = " + delete);
    }

}
